﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFTray
{
	public partial class WndConfigRecipe : Form
	{
		public WndConfigRecipe()
		{
			InitializeComponent();
			TopLevel = false;

			VisibleChanged += (s, e) =>
			  {
				  if (Visible == false) return;
				  textBoxRecipe.Text = GlobalParams.CD.GetRecipeFile();
			  };
		}

		internal void OnUiTimer()
		{
		}

		

		private void buttonSaveAs_Click(object sender, EventArgs e)
		{
			DlgSaveAsRecipe dlg = new DlgSaveAsRecipe();
			dlg.ShowDialog(this);
		}

		
		private void buttonLoad_Click(object sender, EventArgs e)
		{
			//string name = Path.GetFileNameWithoutExtension(recipe_file);
			//string path = Path.GetDirectoryName(recipe_file);
			//path = Path.Combine(path, "PLC");
			//if (Directory.Exists(path) == false) Directory.CreateDirectory(path);
			//string plcf = Path.Combine(path, name + ".txt");
			//return plcf;

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.InitialDirectory = GlobalParams.CD.RecipeBootPath;
			dlg.Filter = "recipe file (.xml)|*.xml|all file(*.*)|*.*";
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				try
				{
					var crf = ConfigUtility.ConfigurationBase<Config.ConfigurationRecipe>.Load(dlg.FileName);
					GlobalParams.CRF = crf;
					GlobalParams.CR = crf.CFG;
					
					FileInfo fi = new FileInfo(dlg.FileName);
					GlobalParams.CD.RecipeName = Path.GetFileNameWithoutExtension(fi.Name);
					GlobalParams.CDF.Save();

					textBoxRecipe.Text = GlobalParams.CD.GetRecipeFile();

					GlobalParams.PC.updatemark();

					GlobalParams.PC.UpdateCvParams();
				}
				catch(Exception ex)
				{
					MessageBox.Show(this, "Failed to Load Recipe" + Environment.NewLine + ex.Message, "ERROR");
				}
			}
		}
	}
}
